import axios, { AxiosResponse } from 'axios'
import { ElMessage } from 'element-plus'

const instance = axios.create({ timeout: 60000 })

// 请求拦截
// @ts-ignore
instance.interceptors.request.use(
  (config) => {
      return config
  },
  (error: any) => {
    Promise.reject(error)
  }
)

// 响应拦截
instance.interceptors.response.use(
  async (response: AxiosResponse) => {
    console.log(response, 'successRes')
    const { status, statusText } = response
    if (!status || !statusText) {
        ElMessage.error('请求失败，请重试!')
        return Promise.reject(response)
    }
    if (![200, 204].includes(status)) {
        ElMessage.error(statusText)
        return Promise.reject(response)
    }
    return response
  },
  (error: any) => {
    // do something
    console.log(error, 'errorResponse')
    const { response: { statusText } } = error
    ElMessage.error(statusText)
    return Promise.reject(error)
  }
)

export default instance
